package com.hpu.callback;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.hpu.constants.HttpConstants;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 实现了登录业务类的回调接口
 * @author Administrator
 *
 */
public class LoginCallback implements Callback{

	private Handler handler;

	String vpnPw="";
	
	public LoginCallback(Handler handler) {
		this.handler=handler;
	}

	public LoginCallback(Handler handler,String vpnPw) {
		this.handler=handler;
		this.vpnPw=vpnPw;
	}
	
	@Override
	public void onFailure(Call arg0, IOException arg1) {
		handler.sendEmptyMessage(HttpConstants.LOGIN_ERROR);
		arg1.printStackTrace();
	}

	@Override
	public void onResponse(Call arg0, Response arg1) throws IOException {
		String result = arg1.body().string();
		String cookie = arg1.header("set-cookie");
		if (result.equals("1")) {
			Message message = new Message();
			message.what = HttpConstants.LOGIN_SUCCESS;
			message.obj = cookie;
			handler.sendMessage(message);
		}
		
		else if (result.equals("-1")) handler.sendEmptyMessage(HttpConstants.LOGIN_PASSWORD_ERROR);
		
		else if(result.equals("-3")) handler.sendEmptyMessage(HttpConstants.LOGIN_JWPASSWORD_ERROR);
		
		else if(result.equals("502")) handler.sendEmptyMessage(HttpConstants.NEED_INFO);
		
		else if(result.equals("501")) handler.sendEmptyMessage(HttpConstants.KEY_ERROR);
		
		else if(result.equals("-4")) handler.sendEmptyMessage(HttpConstants.LOGIN_ERROR);
		
		else if(result.equals("-5")) handler.sendEmptyMessage(HttpConstants.LOGIN_PEOPLE_OVER);
		
		else handler.sendEmptyMessage(HttpConstants.NOEXPECT);
		
		arg1.close();
		arg1.body().close();
	}
}
